﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace Glucometer
{
    public enum ButtonClickType
    { 
        SHORT_CLICK,
        DOUBLE_CLICK,
        LONG_CLICK,
        UNDEFINED
    }

    public delegate void d_ButtonClick(ButtonClickType clickType);

    class ButtonClickInterpreter
    {
        public event d_ButtonClick ButtonClick;


        Timer _timer;
        Timer _singleClickTimer;
        DateTime _timerStartDateTime;
        DateTime _lastShortClickDateTime;

        private int LONG_PUSH_LOWER_LIMIT = 2000;
        private int LONG_PUSH_UPPER_LIMIT = 4000;
        private int SHORT_PUSH_LIMIT = 500;
        private int TIME_LIMIT_BETWEEN_TWO_SHORT_CLICK = 500;

        public ButtonClickInterpreter()
        {
            _timer = new Timer();
            _singleClickTimer = new Timer();
            _singleClickTimer.Interval = TIME_LIMIT_BETWEEN_TWO_SHORT_CLICK;
            _singleClickTimer.Elapsed += new ElapsedEventHandler(singleClickTimer_Elapsed);
        }

        void singleClickTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //THIS IS SHORT CLICK
            _singleClickTimer.Stop();
            ButtonClick(ButtonClickType.SHORT_CLICK);
        }

        public void HandleHardwareInterrupt(HardwareInterrupt interrupt, DateTime interruptTime)
        {
            if (interrupt == HardwareInterrupt.BUTTON_UP)
            {
                double interval = StopTimer();

                if (interval < SHORT_PUSH_LIMIT) //THIS IS SHORT CLICK CONDITION
                {
                    double intervalBetweenClicks = interruptTime.Subtract(_lastShortClickDateTime).TotalMilliseconds;
                    //CHECK IF IT MAKES DOUBLE CLICK
                    if (intervalBetweenClicks < TIME_LIMIT_BETWEEN_TWO_SHORT_CLICK)
                    {
                        //THIS IS DOUBLE CLICK
                        _singleClickTimer.Stop();
                        ButtonClick(ButtonClickType.DOUBLE_CLICK);
                    }
                    else
                    {
                        //THIS IS SHORT CLICK SO WAIT TO CHECK IF THERE IS ANOTHER SINGLE CLICK COMING
                        //BECAUSE IT CAN BE DOUBLE CLICK.
                        _lastShortClickDateTime = interruptTime;
                        _singleClickTimer.Start();
                    }
                }
                else if (interval > LONG_PUSH_LOWER_LIMIT &&
                    interval < LONG_PUSH_UPPER_LIMIT)
                {
                    //THIS IS LONG PUSH CONDITION
                    ButtonClick(ButtonClickType.LONG_CLICK);
                }
                else
                {
                    //UNDEFINED BUTTON CLICK
                    ButtonClick(ButtonClickType.UNDEFINED);
                }
            }
            else if (interrupt == HardwareInterrupt.BUTTON_DOWN)
            {
                RestartTimer();
            }
        }

        private void RestartTimer()
        {
            StartTimer();
        }

        private DateTime StartTimer()
        {
            _timerStartDateTime = DateTime.Now;
            _timer.Start();
            return _timerStartDateTime;
        }

        private double StopTimer()
        {
            _timer.Stop();
            return getTimerElapseTime();

        }

        private double getTimerElapseTime()
        {
            return DateTime.Now.Subtract(_timerStartDateTime).TotalMilliseconds;
        }
    }
}